<?php

namespace App\Http\Controllers;


use App\Good;
use App\Goodsfirst;
use App\Goodssecond;
use App\GoodsTask;
use App\Goodsthird;
use Excel;
use PHPExcel_IOFactory;
use PHPExcel_Cell;

class ExcelController extends Controller
{
    public function index()
    {
        Excel::load('222.xls', function($reader) {

           $data= $reader->all();

            foreach ($data as $d){
                $input['name']=$d[1];
                $input['goodsCode']=$d[0];
                $input['goodsType']=$d[2];
                $input['goodsUnit']=$d[3];
                $input['goodsNotes']=$d[4];
                if($d[5] == 1){
                    Goodsfirst::create($input);
                }elseif($d[5] == 2){
                    Goodssecond::create($input);
                }elseif($d[5] == 3){
                    Goodsthird::create($input);
                }
            }

        });
    }

    public function set()
    {
       // Good::where('unit','件')->update(['bom'=>1]);
       // Good::where('goodsType','like','%x%')->update(['bom'=>3]);
//        Good::where('name','like','%8%')->where('bom',1)->update(['bom2'=>8]);
//        Good::where('name','like','%12%')->where('bom',1)->update(['bom2'=>12]);
//        Good::where('name','like','%18%')->where('bom',1)->update(['bom2'=>18]);
//        Good::where('name','like','%24%')->where('bom',1)->update(['bom2'=>24]);
//        Good::where('name','like','%36%')->where('bom',1)->update(['bom2'=>36]);
//        Good::where('name','like','%48%')->where('bom',1)->update(['bom2'=>48]);
//
//
//        Good::where('goodsCode','like','%C01%')->where('bom',3)->update(['bom2'=>1]);
//        Good::where('goodsCode','like','%C02%')->where('bom',3)->update(['bom2'=>2]);
//        Good::where('goodsCode','like','%C03%')->where('bom',3)->update(['bom2'=>3]);
//        Good::where('goodsCode','like','%C04%')->where('bom',3)->update(['bom2'=>4]);
//        Good::where('goodsCode','like','%C05%')->where('bom',3)->update(['bom2'=>5]);
//        Good::where('goodsCode','like','%C06%')->where('bom',3)->update(['bom2'=>6]);
//        Good::where('goodsCode','like','%C07%')->where('bom',3)->update(['bom2'=>7]);
//        Good::where('goodsCode','like','%C08%')->where('bom',3)->update(['bom2'=>8]);
//        Good::where('goodsCode','like','%C09%')->where('bom',3)->update(['bom2'=>9]);
//        Good::where('goodsCode','like','%C10%')->where('bom',3)->update(['bom2'=>10]);
//        Good::where('goodsCode','like','%C11%')->where('bom',3)->update(['bom2'=>11]);
//        Good::where('goodsCode','like','%C12%')->where('bom',3)->update(['bom2'=>12]);
//        Good::where('goodsCode','like','%C13%')->where('bom',3)->update(['bom2'=>13]);
//        Good::where('goodsCode','like','%C14%')->where('bom',3)->update(['bom2'=>14]);
//        Good::where('goodsCode','like','%C15%')->where('bom',3)->update(['bom2'=>15]);
//        Good::where('goodsCode','like','%C16%')->where('bom',3)->update(['bom2'=>16]);
//        Good::where('goodsCode','like','%C17%')->where('bom',3)->update(['bom2'=>17]);
//        Good::where('goodsCode','like','%C18%')->where('bom',3)->update(['bom2'=>18]);
//        Good::where('goodsCode','like','%C19%')->where('bom',3)->update(['bom2'=>19]);
//        Good::where('goodsCode','like','%C20%')->where('bom',3)->update(['bom2'=>20]);
//        Good::where('goodsCode','like','%C21%')->where('bom',3)->update(['bom2'=>21]);
//        Good::where('goodsCode','like','%C22%')->where('bom',3)->update(['bom2'=>22]);
//        Good::where('goodsCode','like','%C23%')->where('bom',3)->update(['bom2'=>23]);
//        Good::where('goodsCode','like','%C24%')->where('bom',3)->update(['bom2'=>24]);
//        Good::where('goodsCode','like','%C25%')->where('bom',3)->update(['bom2'=>25]);
//        Good::where('goodsCode','like','%C26%')->where('bom',3)->update(['bom2'=>26]);
//        Good::where('goodsCode','like','%C27%')->where('bom',3)->update(['bom2'=>27]);
//        Good::where('goodsCode','like','%C28%')->where('bom',3)->update(['bom2'=>28]);
//        Good::where('goodsCode','like','%C29%')->where('bom',3)->update(['bom2'=>29]);
//        Good::where('goodsCode','like','%C30%')->where('bom',3)->update(['bom2'=>30]);
//        Good::where('goodsCode','like','%C31%')->where('bom',3)->update(['bom2'=>31]);
//        Good::where('goodsCode','like','%C32%')->where('bom',3)->update(['bom2'=>32]);
//        Good::where('goodsCode','like','%C33%')->where('bom',3)->update(['bom2'=>33]);
//        Good::where('goodsCode','like','%C34%')->where('bom',3)->update(['bom2'=>34]);
//        Good::where('goodsCode','like','%C35%')->where('bom',3)->update(['bom2'=>35]);
//        Good::where('goodsCode','like','%C36%')->where('bom',3)->update(['bom2'=>36]);
//        Good::where('goodsCode','like','%C37%')->where('bom',3)->update(['bom2'=>37]);
//        Good::where('goodsCode','like','%C38%')->where('bom',3)->update(['bom2'=>38]);
//        Good::where('goodsCode','like','%C39%')->where('bom',3)->update(['bom2'=>39]);
//        Good::where('goodsCode','like','%C40%')->where('bom',3)->update(['bom2'=>40]);
//        Good::where('goodsCode','like','%C41%')->where('bom',3)->update(['bom2'=>41]);
//        Good::where('goodsCode','like','%C42%')->where('bom',3)->update(['bom2'=>42]);
//        Good::where('goodsCode','like','%C43%')->where('bom',3)->update(['bom2'=>43]);
//        Good::where('goodsCode','like','%C44%')->where('bom',3)->update(['bom2'=>44]);
//        Good::where('goodsCode','like','%C45%')->where('bom',3)->update(['bom2'=>45]);
//        Good::where('goodsCode','like','%C46%')->where('bom',3)->update(['bom2'=>46]);
//        Good::where('goodsCode','like','%C47%')->where('bom',3)->update(['bom2'=>47]);
//        Good::where('goodsCode','like','%C48%')->where('bom',3)->update(['bom2'=>48]);
       // Good::whereNull('bom')->update(['bom'=>2]);
       // Good::where('bom',1)->update(['rank'=>1]);
      //  Good::where('bom',2)->update(['rank'=>2]);
//        Good::where('name','like','%笔头%')->update(['bom2'=>99,'bom'=>3]);
//        Good::where('name','like','%笔尖%')->update(['bom2'=>99,'bom'=>3]);
//        Good::where('name','like','%尾塞%')->update(['bom2'=>99,'bom'=>3]);
//        Good::where('name','like','%笔盖%')->update(['bom'=>3]);
//        Good::where('name','like','%包芯%')->update(['bom'=>3]);
//        $goods=Good::where('bom',1)->get();
//        foreach ($goods as $g){
//            $goodsfirst=Good::find($g->id);
//            $goodsSend=Good::where('goodsType',$g->goodsType)->where('bom',2)->get();
//            foreach ($goodsSend as $gs){
//                if ($gs->bom2 <= $g->bom2){
//                    $goodsfirst->children()->attach($gs->id);
//                }
//            }
//            if (in_array($g->goodsType,['1661','1662','1663','1664','1665','1666','1667'])){
//                $goodsSend=Good::where('goodsType','1661')->where('bom',2)->get();
//                foreach ($goodsSend as $gs){
//                    if ($gs->bom2 <= $g->bom2){
//                        $goodsfirst->children()->attach($gs->id);
//                    }
//                }
//            }
//        }

        $goods=Good::where('bom',2)->where('goodsCode','like','%155%')->get();

        foreach ($goods as $g){
            $goodsfirst=Good::find($g->id);
            $goodsSend=Good::where('name','like','%笔头%')->where('goodsCode','like','%'.$g->goodsType.'%')->whereIn('bom2',[$g->bom2,99])->where('bom',3)->get();

            foreach ($goodsSend as $gs){
                    $goodsfirst->children()->attach($gs->id);
            }
        }

//        $goods=Good::where('bom',3)->where('goodsCode','like','%199%')->get();
//
//        foreach ($goods as $g){
//            $goodsfirst=Good::find($g->id);
//            $goodsfirst->parent()->detach(985);
//            $goodsfirst->parent()->detach(986);
//            $goodsfirst->parent()->detach(987);
//            $goodsfirst->parent()->detach(988);
//            $goodsfirst->parent()->detach(1136);
//            $goodsfirst->parent()->detach(1137);
//            $goodsfirst->parent()->detach(1138);
//            $goodsfirst->parent()->detach(1139);
//        }

    }
    public function secondToFirst()  //关联second和first关系
    {
        $goodsfirsts=Goodsfirst::all();
        $goodsseconds=Goodssecond::all();
        foreach ($goodsfirsts as $gf){
            foreach($goodsseconds as $gs){
                if($gs->goodsType == $gf->goodsType and $gs->goodsNotes <= $gf->goodsNotes){
                    $goodsfirst=Goodsfirst::find($gf->id);
                    $goodsfirst->goodsseconds()->attach($gs->id);
                }
            }
        }

    }

    public function thirdToSecond()
    {

        $seconds=Goodssecond::all();
        $thirds=Goodsthird::all();
        foreach ($seconds as $gs){
            $var=mb_substr($gs->name,3,20,'utf-8');

            foreach ($thirds as $gt) {

                if (str_contains($gt->goodsType, 'X')) {
                    $i = 'ture';
                    echo $gt->goodsType . '@' . $i . '<br>';
                    if (str_contains($gt->name, $var) and mb_substr($gt->goodsType, 0, 3, 'utf-8') == mb_substr($gs->goodsType, 0, 3, 'utf-8') and $gt->goodsNotes <= $gs->goodsNotes and $gt->goodsNotes != 0) {

                        $second = Goodssecond::find($gs->id);
                        $second->goodsthirds()->attach($gt->id);
                    } elseif (mb_substr($gt->goodsType, 0, 3, 'utf-8') == mb_substr($gs->goodsType, 0, 3, 'utf-8') and $gt->goodsNotes == 0) {
                        $second = Goodssecond::find($gs->id);
                        $second->goodsthirds()->attach($gt->id);

                    }
                } else {
                    $i = 'false';
                    echo $gt->goodsType . '@' . $i . '<br>';
                    if (str_contains($gt->name, $var) and $gt->goodsType == $gs->goodsType and $gt->goodsNotes <= $gs->goodsNotes and $gt->goodsNotes != 0) {

                        $second = Goodssecond::find($gs->id);
                        $second->goodsthirds()->attach($gt->id);
                    } elseif ($gt->goodsType == $gs->goodsType and $gt->goodsNotes == 0) {
                        $second = Goodssecond::find($gs->id);
                        $second->goodsthirds()->attach($gt->id);

                    }
                }


            }
        }

    }


}
